/**
 * StarNodeList.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2010-2012 Roslan Amir. All rights reserved.
 */

package name.roslan.games.moo3d.spatial;

import java.util.ArrayList;

/**
 * Extends the <code>ArrayList</code> for <code>StarNode</code> with automatic sorting during inserts.
 * 
 * @author Roslan Amir
 * @date May 28, 2009
 */
public class StarNodeList extends ArrayList<StarNode> {

	private static final long serialVersionUID = 1L;

	/**
	 * Default no-arg constructor.
	 */
	public StarNodeList() {
		super(24); // smallest number of stars is 24 for a small galaxy
	}

	/**
	 * Add a new <code>StarNode</code> to the list. This node is sorted in the order of the node's tile coordinates.
	 * 
	 * @see java.util.ArrayList#add(java.lang.Object)
	 */
	@Override
	public boolean add(StarNode node) {
		for (int index = 0; index < this.size(); index++) {
			StarNode node2 = this.get(index);
			if (node.tile.y < node2.tile.y) {
				super.add(index, node);
				return true;
			}
			if (node.tile.y == node2.tile.y) {
				if (node.tile.x < node2.tile.x) {
					super.add(index, node);
					return true;
				}
			}
		}
		super.add(node);
		return true;
	}

	/**
	 * Returns the <code>StarNode</code> with the given name.
	 * 
	 * @param name the star name
	 * @return the <code>StarNode</code> with the given name, null if no star with the name exists
	 */
	public StarNode get(String name) {
		for (StarNode node : this) {
			if (node.getName().equals(name)) {
				return node;
			}
		}
		return null;
	}

}
